package com.chenyue.cm.user.controller;


import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chenyue.cm.base.aop.Log;
import com.chenyue.cm.base.bean.PageVo;
import com.chenyue.cm.base.bean.Pageable;
import com.chenyue.cm.bean.Success;
import com.chenyue.cm.common.constant.Constants;
import com.chenyue.cm.common.utils.RequestUtil;
import com.chenyue.cm.user.domain.*;
import com.chenyue.cm.user.domain.vo.OrderDetailVo;
import com.chenyue.cm.user.service.*;
import com.chenyue.cm.utils.DateUtils;
import com.tencentcloudapi.scf.v20180416.models.CreateTriggerRequest;
import com.tencentcloudapi.tcb.v20180608.models.OrderInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author chenyue-cm
* @date 2024-02-29
*/
@RestController
@RequestMapping("/jdOrderInfo")
public class JdOrderInfoController {

    private final JdOrderInfoService jdOrderInfoService;

    @Resource
    private JdMemberAddressService addressService;
    @Resource
    private JdGoodsSpuService jdGoodsSpuService;
    @Resource
    private JdGoodsSkuService jdGoodsSkuService;


    public JdOrderInfoController(JdOrderInfoService jdOrderInfoService) {
        this.jdOrderInfoService = jdOrderInfoService;
    }

    @RequestMapping("/getList")
    public Success getList(HttpServletRequest request) {
        Pageable pageInfo = RequestUtil.getPageable(request);
		PageVo<JdOrderInfo>  result  = jdOrderInfoService.getPageBean(pageInfo, JdOrderInfo.class);
		return Success.success(result);
    }

    /**
     * @param vo 插入实体
     *
     * @return Success
     */
    @Log
    @RequestMapping("/save")
    public Success save(JdOrderInfo vo) {
        boolean success = jdOrderInfoService.save(vo);
        return success ? Success.success() : Success.error();
    }

    /**
    * @param vo 修改实体
    * @param request id
    *
    * @return Success
    */
   @Log
   @RequestMapping("/edit")
   public Success edit(JdOrderInfo vo, HttpServletRequest request) {
       String id = request.getParameter("id");
        if (StringUtils.isNotBlank(id)) {
            int success = jdOrderInfoService.getBaseMapper().updateById(vo);
            return success > 0 ? Success.success() : Success.error();
        } else {
          return Success.error("id不能为空");
        }
    }

    /**
     *
     * @param id 主键
     *
     * @return Success
     */
    @Log
    @RequestMapping("/delete/{id}")
    public Success deleteByPrimaryKey(@PathVariable("id") Integer id) {
        boolean success = jdOrderInfoService.removeById(id);
        return success ? Success.success() : Success.error();
    }

 	/**
	 * 【API】----  删除多个数据
     *
	 * @return Success
	 */
    @Log
    @RequestMapping("/delete")
	public Success delete(String ids) {
        String[] idStrings = ids.split(",");
        for (String idString : idStrings) {
            int success = jdOrderInfoService.deleteByPrimaryKey(idString);
            if (success == 0) {
                return Success.error();
            }
        }
        return Success.success();
	}


    @RequestMapping("/sendOrder")
    @ResponseBody
    public Success sendOrder(String id) {
        if (StringUtils.isEmpty(id)) {
            return Success.error("订单ID不能为空");
        }
        // 修改订单状态
        JdOrderInfo mOrderInfo = this.jdOrderInfoService.getById(id);
        mOrderInfo.setOrderStatus(Constants.ORDER_STATUS_2);
        this.jdOrderInfoService.updateById(mOrderInfo);

        return Success.success();
    }

    @RequestMapping("/saveLogistics")
    @ResponseBody
    @Log
    public Success save(String logisticsNum,String companyCode,String orderId) {
        if(orderId != null && orderId != "") {
            JdOrderInfo orderInfo = jdOrderInfoService.getById(orderId);
            orderInfo.setLogisticsId(logisticsNum);
            orderInfo.setLogisticsFee(companyCode);
            orderInfo.setDeliveryTime(DateUtil.date().toStringDefaultTimeZone());
            jdOrderInfoService.updateById(orderInfo);
        }else {
            return Success.error();
        }
        return Success.success();
    }


    /**
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/getOrderDetailById")
    @ResponseBody
    public Success<OrderDetailVo> getOrderDetailById(String orderId) {

        OrderDetailVo orderDetailVo = jdOrderInfoService.getorderlist(orderId);
        return Success.success(orderDetailVo);
    }


    @GetMapping("/orderDispatchBill")
    public Success orderDispatchBill(String orderId){
        if (StringUtils.isEmpty(orderId)) return Success.error("订单Id不能为空");

        JdOrderInfo orderInfo = jdOrderInfoService.selectByPrimaryKey(orderId);
        if (orderInfo == null) return Success.error("订单为空");

        //查询收货人信息
        JdMemberAddress memberAddress = addressService.getById(orderInfo.getAddressId());
        if (memberAddress == null) return Success.error("用户地址为空");

        Map map = new HashMap();
        map.put("buyerName", memberAddress.getBuyerName());
        map.put("buyerPhone", memberAddress.getBuyerPhone());
        map.put("orderNo", orderInfo.getId());
        map.put("addressDetail", memberAddress.getAddressDetail());
        map.put("remark", StringUtils.isEmpty(orderInfo.getRemark())?"无":orderInfo.getRemark());

        map.put("num", "1");
        JdGoodsSpu spu = jdGoodsSpuService.getById(orderInfo.getSpuId());
        JdGoodsSku sku = jdGoodsSkuService.getById(orderInfo.getSkuId());
        map.put("goodName", spu.getGoodName());
        map.put("sku", sku.getSkuImgurl());
        map.put("skuPrice", sku.getSkuPrice());
        map.put("count", sku.getSkuStock());
        map.put("orderMoney", sku.getSkuSaleprice());
        map.put("totalCount", orderInfo.getGoodsCount());
        map.put("totalMoney", orderInfo.getOrderMoney());
        return Success.success(map);
    }

}
